package com.example.happ.controller;


import com.example.happ.pojo.Price;
import com.example.happ.pojo.Store;
import com.example.happ.service.StoreService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/store")
public class storeController {
    @Resource
    private StoreService storeService;
    @RequestMapping("/update")//门店信息主页面保存请求
    @CrossOrigin(origins = "http://localhost:8080")
    public boolean updateStore(@RequestBody Map<String, String> requestBody) {
        System.out.println("门店信息主页面保存请求收到的数据："+requestBody);
        return storeService.updateStore(requestBody);
    }
    @RequestMapping("/update1")//门店信息子页面保存请求
    @CrossOrigin(origins = "http://localhost:8080")
    public Store update1Store(@RequestBody Map<String, String> requestBody) {
        System.out.println("门店信息子页面保存请求收到的数据："+requestBody);
        return storeService.updateStore1(requestBody);
    }
    @RequestMapping("/update2")//随机红包保存请求
    @CrossOrigin(origins = "http://localhost:8080")
    public boolean updatePrice(@RequestBody Map<String, String> requestBody) {
        boolean success = false;
        System.out.println("随机红包保存请求收到的数据:"+requestBody);
        Map<String, String> request = new HashMap<>();
        request.put("type",requestBody.get("type"));
        request.put("storeId",requestBody.get("storeId"));
        Price price = getPrice(request);
        System.out.println("随机红包信息访问请求返回的数据2:"+price);
        if (price == null) {
            success=storeService.insertPrice(requestBody);
        }else {
            success=storeService.updatePrice(requestBody,price);
        }
        System.out.println(success);
        return success;
    }
    @RequestMapping("/find1")//随机红包信息访问请求
    @CrossOrigin(origins = "http://localhost:8080")
    public Price getPrice(@RequestBody Map<String, String> requestBody){
        System.out.println("随机红包信息访问请求收到的数据:"+requestBody);
        Price price=storeService.findPrice(requestBody.get("type"),requestBody.get("storeId"));
        System.out.println("随机红包信息访问请求返回的数据:"+price);
        return price;
    }
    @RequestMapping("/find")//第一次门店信息请求
    @CrossOrigin(origins = "http://localhost:8080")
    public Store getMerchant(@RequestBody Map<String, Integer> requestBody){
        System.out.println("主门店信息请求收到的id:"+requestBody.get("id"));
        Store store=storeService.findStore(requestBody.get("id"));
        System.out.println("主门店信息请求返回的对象:"+store);
        return store;
    }
}
